package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.InFo.HeroSoundInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/7.
 */
public class HeroSoundAdapter extends BaseAdapter {
    private HeroSoundInfo heroSoundInfo;
    private Context context;


    public HeroSoundAdapter(HeroSoundInfo heroSoundInfo, Context context) {
        this.heroSoundInfo = heroSoundInfo;
        this.context = context;
    }

    @Override
    public int getCount() {
        return heroSoundInfo.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return heroSoundInfo.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder viewHolder = null;
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_soundlistview, null);
            viewHolder.tv_sound = (TextView) convertView.findViewById(R.id.tv_sound);
            viewHolder.btn_play = (Button) convertView.findViewById(R.id.soundplay);
            viewHolder.btn_music= (Button)convertView.findViewById(R.id.btn_music);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final HeroSoundInfo.DataBean bean = heroSoundInfo.getData().get(position);
        viewHolder.tv_sound.setText(bean.getName());
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.btn_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalViewHolder.btn_music.setVisibility(View.VISIBLE);
                String url =bean.getPath_url();
                MediaPlayer mediaPlayer = new MediaPlayer();
                try {
                    mediaPlayer.setDataSource(url);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });
        return convertView;
    }

     private class ViewHolder {
        private TextView tv_sound;
        private Button btn_play,btn_music;

    }

}


